package com.example.memo1.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.memo1.R;
import com.example.memo1.dao.AdminDao;

public class RegisterActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //返回登录页面
        Button to_login=findViewById(R.id.to_login);
        to_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });


        EditText zh=findViewById(R.id.reg_zh);//账号
        EditText pwd=findViewById(R.id.reg_pwd);//密码
        EditText name=findViewById(R.id.name);//姓名
        RadioButton man=findViewById(R.id.register_man);//男
        RadioButton woman=findViewById(R.id.register_woman);//女
        EditText phone=findViewById(R.id.phone);//联系方式
        EditText age=findViewById(R.id.age);//年龄

        Button button=findViewById(R.id.reg_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String zhT=zh.getText().toString().trim();
                String pwdT=pwd.getText().toString().trim();
                String nameT=name.getText().toString().trim();
                String phoneT=phone.getText().toString().trim();
                String ageT=age.getText().toString().trim();

                if(zhT.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
                } else if (pwdT.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else if (nameT.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "请输入姓名", Toast.LENGTH_SHORT).show();
                } else if (phoneT.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "请输入联系方式", Toast.LENGTH_SHORT).show();
                } else if (ageT.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "请输入年龄",Toast.LENGTH_SHORT).show();

                }else{
                    String sex="女";
                    if(man.isChecked()){
                        sex="男";
                    }
                    AdminDao adminDao=new AdminDao();
                   int a= adminDao.registerAdmin(zhT,pwdT,nameT,sex,phoneT,ageT);
                    if(a==1){
                        Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
                    }
                }


            }
        });


    }
}